﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System.Windows.Forms;

    public static class PugMessageBox
    {
        public static PugDialogResult Show(string message)
        {
            return CreateOk(message);
        }

        public static PugDialogResult Show(string message, string caption)
        {
            return CreateOk(message, caption);
        }

        public static PugDialogResult Show(IWin32Window parent, string message)
        {
            return CreateOk(message, string.Empty, parent);
        }

        public static PugDialogResult Show(IWin32Window parent, string message, string caption)
        {
            return CreateOk(message, caption, parent);
        }

        public static PugDialogResult Show(string message, string caption, PugDialogButtons buttons)
        {
            return CreateDialog(message, caption, buttons, null, PugDialogIcon.None);
        }

        public static PugDialogResult Show(IWin32Window parent, string message, string caption, PugDialogButtons buttons)
        {
            return CreateDialog(message, caption, buttons, parent, PugDialogIcon.None);
        }

        public static PugDialogResult Show(string message, string caption, PugDialogButtons buttons, PugDialogIcon icon)
        {
            return CreateDialog(message, caption, buttons, null, icon);
        }

        public static PugDialogResult Show(IWin32Window parent, string message, string caption, PugDialogButtons buttons, PugDialogIcon icon)
        {
            return CreateDialog(message, caption, buttons, parent, icon);
        }

        private static PugDialogResult CreateDialog(string message, string caption, PugDialogButtons buttons, IWin32Window parent, PugDialogIcon icon)
        {
            PugDialogResult result;
            using (PugDialog dialog = new PugDialog())
            {
                dialog.AddButtons(PugDialogFactory.CreateButtons(buttons));
                dialog.SetCaption(caption);
                dialog.SetMessage(message);
                dialog.SetImage(PugDialogFactory.CreateImage(icon));

                if (parent == null)
                {
                    dialog.ShowDialog();
                }
                else
                {
                    dialog.ShowDialog(parent);
                }
                result = dialog.Result;
            }
            return result;
        }


        private static PugDialogResult CreateOk(string message, string caption, IWin32Window parent)
        {
            PugDialogResult result;
            using (PugDialog dialog = new PugDialog())
            {
                dialog.AddButton(PugDialogFactory.Ok);
                dialog.Text = caption;
                dialog.SetMessage(message);
                if (parent == null)
                {
                    dialog.ShowDialog();
                }
                else
                {
                    dialog.ShowDialog(parent);
                }
                result = dialog.Result;
            }
            return result;
        }

        private static PugDialogResult CreateOk(string message, string caption)
        {
            return CreateOk(message, caption, null);
        }

        private static PugDialogResult CreateOk(string message)
        {
            return CreateOk(message, string.Empty);
        }
    }
}